package com.example.wangshuo.zyfypt_205ws.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.wangshuo.zyfypt_205ws.R;
import com.example.wangshuo.zyfypt_205ws.adapter.TcaseAdapter;
import com.example.wangshuo.zyfypt_205ws.bean.SessionidBean;
import com.example.wangshuo.zyfypt_205ws.bean.TcaseBean;
import com.example.wangshuo.zyfypt_205ws.iface.TcaseListener;
import com.example.wangshuo.zyfypt_205ws.model.ArticleModel;
import com.example.wangshuo.zyfypt_205ws.model.TcaseModel;

import java.util.List;

/**
 * Created by wangshuo on 2017/4/29.
 */

public class Tcase extends Fragment {

    private View view=null;
    private Context context;
    private RecyclerView recyclerView;
   // private RecyclerView.LayoutManager layoutManager;//RecyclerView布局管理
    private TcaseAdapter myAdapter;//RecyclerView适配器
    private List<TcaseBean> listTcase=null;//保存数据
    private int page=1;
    private int lastVisibleItemPosition;
    private LinearLayoutManager layoutManager;
    TcaseListener tcaseListener=new TcaseListener() {
        @Override
        public void onResponse(List<TcaseBean> beanList) {
            //listTcase=beanList;//拿到文章数据
            if(page==1)
            {
                listTcase=beanList;
            }
            else {
                listTcase.removeAll(beanList);
                listTcase.addAll(beanList);
            }
            myAdapter.setList(listTcase);
            myAdapter.notifyDataSetChanged();//通知更新
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getActivity(),"失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
        //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment2,container,false);
        //返回动态生成的view
        initRecyclerView();
        TcaseModel model=new TcaseModel();
        model.getResultList("tcase",page, SessionidBean.getSessionIDBean,tcaseListener);
        return view;
    }
    private void initRecyclerView() {
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerView);
        //创建默认线性布局
        layoutManager=new LinearLayoutManager(getActivity());
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //实例化Adapter
        myAdapter=new TcaseAdapter(getActivity());
        myAdapter.setList(listTcase);//传进数据
        recyclerView.setAdapter(myAdapter);//绑定
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == listTcase.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    TcaseModel tcaseModel = new TcaseModel();
                    tcaseModel.getResultList("tcase",page,SessionidBean.getSessionIDBean,tcaseListener);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });

    }
}
